﻿Imports System.Runtime.InteropServices

Namespace Widgets

    Delegate Sub Twitter_UpdateDelegate(ByVal result As String)

    Public Class TwitterUpdate(Of TView As {ITwitterUpdateView, Control})
        Inherits WidgetBase(Of TView)

        Friend Event Update(ByVal result As String)

        Public Sub New(ByVal View As TView)
            MyBase.New(View)
        End Sub

        Public Sub Send(ByVal status As String)
            Dim TwiterCredentials = View.GetTwiterCredentials
            Dim result As String = Twitter.TwitterAPI.UpdateStatus(status, TwiterCredentials.Email, TwiterCredentials.Password)
            UpdateFromTimers(result)
        End Sub

        Private Sub UpdateFromTimers(ByVal result As String)
            If View.InvokeRequired Then
                View.Invoke(New Twitter_UpdateDelegate(AddressOf UpdateFromTimers), result)
            Else
                RaiseEvent Update(result)
            End If
        End Sub

    End Class

End Namespace